//
//  YMTabBar.swift
//  YMWeiBo
//
//  Created by mimi on 16/7/19.
//  Copyright © 2016年 mimi. All rights reserved.
//

import UIKit


//        MARK: - 代理的使用

protocol YMTabBarDelegate:NSObjectProtocol {
    
    func composeButtonClick()
    
}

class YMTabBar: UITabBar {
    //声明代理
    weak var ymDelegate:YMTabBarDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    override func layoutSubviews() {
        
        edit_bt.center.x  = frame.width*0.5
        edit_bt.center.y = frame.height*0.5
        
        let w = frame.width*0.2
        var index:CGFloat = 0
        
        for value in subviews{

            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                value.frame.origin.x = index*w
                value.frame.size.width = w
                index++
                if index == 2{
                    index++
                }
                
            }
            
        }
        
    }
    
    
    
    func setupUI() {
        addSubview(edit_bt)
  
        
    }
    
    //        MARK: - =========== 响应事件 ===========
    
    func didClickBt() {
        
        editClosure?()
        
//        ymDelegate?.composeButtonClick()
    }
    
    //        MARK: - ======声明属性=======
   var editClosure:(() ->())?
    
    //        MARK: -============= 懒加载控件 =============
    
    lazy var edit_bt:UIButton = {
       let bt = UIButton()
        bt.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: .Normal)
        bt.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: .Highlighted)
        bt.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: .Normal)
        bt.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        bt.sizeToFit()
        
        bt.addTarget(self, action: #selector(YMTabBar.didClickBt), forControlEvents: .TouchUpInside)
        
        return bt
    }()
}
